AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')

function ENT:Initialize()

	self:SetModel( "models/zombie/classic.mdl" )
	util.PrecacheSound("npc/zombie/zo_attack2.wav")
	
	self:SetHullType( HULL_HUMAN );
	self:SetHullSizeNormal();
	
	self:SetSolid( SOLID_BBOX ) 
	self:SetMoveType( MOVETYPE_STEP )
	
	self:CapabilitiesAdd( CAP_MOVE_GROUND | CAP_ANIMATEDFACE | CAP_TURN_HEAD | CAP_USE_SHOT_REGULATOR | CAP_AIM_GUN )
	
	self:SetMaxYawSpeed( 5000 )
	
	self:SetHealth(math.random(90,110))

end

function ENT:VoiceSound(sound,vol)
	vol = vol or 100
	local rnum = math.random(90,100)
	if self.SoundDelay then
		if self.SoundDelay < CurTime() then
			self.SoundDelay = CurTime() + 1.5
			self:EmitSound(sound,vol,rnum)
			WorldSound( sound, self:GetPos(), 75, rnum )
		end
	else
		self.SoundDelay = CurTime() + 1.5
		WorldSound( sound, self:GetPos(), 75, rnum )
	end
end

function ENT:Team()
	return TEAM_UNDEAD
end	

ENT.Rifles = {"models/weapons/w_snip_awp.mdl",
"models/weapons/w_snip_scout.mdl",
"models/weapons/w_annabelle.mdl",
"models/weapons/w_shot_xm1014.mdl",
"models/weapons/w_shotgun.mdl"}

function ENT:CalculateKiller(dmginfo,ragdoll)
	local killer = dmginfo:GetAttacker()
	local olddist = killer:GetAwardPoints(AWARD_GOOD_DISTANCE) or 0
	local newdist = killer:GetPos():Distance(self:GetPos()) / 16
	if newdist > olddist then
		killer:SetAwardPoints(AWARD_GOOD_DISTANCE,newdist)
	end
	local weapon = killer:GetActiveWeapon()
	local worldmodel = "h"
	if weapon != NULL and weapon != nil then
		worldmodel = weapon:GetWorldModel()
	end
	if GameInfo.KillCalculator == "lastkiller" then
		local tbl = self:GetAttackerTable()
		local headshot = false
		for k,v in pairs(tbl) do
			if v.Player == killer then
				if v.Headshot then
					headshot = true
				end
			end
		end
		killer:AddAwardPoints(AWARD_GOOD_KILLS,1)
		killer:AddAwardPoints(AWARD_BAD_KILLS,1)
		if headshot then
			//killer:AddPoints(Points[self.Name] + 1)
			self:SpawnPlutonium(Points[self.Name] + 1,killer)
			killer:AddAwardPoints(AWARD_GOOD_HEADSHOT,1)
			killer:AddAwardPoints(AWARD_BAD_HEADSHOT,1)
			if (math.random(1,3) == 1 or table.HasValue(self.Rifles,worldmodel)) and self.Name == "Undead" then //HEAD ASPLOSIONS
				ragdoll:SetNWBool("headless",true)
				local effectdata = EffectData()
				effectdata:SetOrigin(ragdoll:GetPos() + Vector(0,0,60))
				util.Effect("head_explode", effectdata)
				local effectdata = EffectData()
				effectdata:SetEntity(ragdoll)
				effectdata:SetOrigin(ragdoll:GetPos())
				util.Effect("bloodspray", effectdata,true,true)
				for i=1,math.random(3,4) do
					local effectdata = EffectData()
					effectdata:SetOrigin(ragdoll:GetPos() + Vector(0,0,60))
					util.Effect("headshot_gib", effectdata,true,true)
				end
			end
		else
			//killer:AddPoints(Points[self.Name])
			self:SpawnPlutonium(Points[self.Name],killer)
		end
	elseif GameInfo.KillCalculator == "mostdamage" then
		local tbl = self:GetAttackerTable()
		local winnar = killer
		local highest = 1
		for k,v in pairs(tbl) do 
			if v.Damage > highest then
				highest = v.Damage
				winnar = v.Player
				headshot = v.Headshot
			end
		end
		winnar:AddAwardPoints(AWARD_GOOD_KILLS,1)
		winnar:AddAwardPoints(AWARD_BAD_KILLS,1)
		if headshot then
			//winnar:AddPoints(Points[self.Name] + 1)
			self:SpawnPlutonium(Points[self.Name] + 1,killer)
			winnar:AddAwardPoints(AWARD_GOOD_HEADSHOT,1)
			winnar:AddAwardPoints(AWARD_BAD_HEADSHOT,1)
			if (math.random(1,3) == 1 or table.HasValue(self.Rifles,worldmodel))and self.Name == "Undead" then
				ragdoll:SetNWBool("headless",true)
				local effectdata = EffectData()
				effectdata:SetOrigin(ragdoll:GetPos() + Vector(0,0,60))
				util.Effect("head_explode", effectdata)
				local effectdata = EffectData()
				effectdata:SetEntity(ragdoll)
				effectdata:SetOrigin(ragdoll:GetPos())
				util.Effect("bloodspray", effectdata,true,true)
				for i=1,math.random(3,4) do
					local effectdata = EffectData()
					effectdata:SetOrigin(ragdoll:GetPos() + Vector(0,0,60))
					util.Effect("headshot_gib", effectdata,true,true)
				end
			end
		else
			//winnar:AddPoints(Points[self.Name])
			self:SpawnPlutonium(Points[self.Name],killer)
		end
	else //points are shared
		local tbl = self:GetAttackerTable()
		local minimum = 20
		for k,v in pairs(tbl) do 
			//Msg("PLAYER:"..v.Player:Name().." DMG:"..v.Damage.." HS:"..tostring(v.Headshot).."\n")
			if v.Damage > minimum or killer == v.Player then
				if v.Headshot then
					//v.Player:AddPoints(Points[self.Name] + 1)
					self:SpawnPlutonium(Points[self.Name] + 1,v.Player)
					v.Player:AddAwardPoints(AWARD_GOOD_HEADSHOT,1)
					v.Player:AddAwardPoints(AWARD_BAD_HEADSHOT,1)
					if (math.random(1,3) == 1 or table.HasValue(self.Rifles,worldmodel)) and self.Name == "Undead" then
						ragdoll:SetNWBool("headless",true)
						local effectdata = EffectData()
						effectdata:SetOrigin(ragdoll:GetPos() + Vector(0,0,60))
						util.Effect("head_explode", effectdata)
						local effectdata = EffectData()
						effectdata:SetEntity(ragdoll)
						effectdata:SetOrigin(ragdoll:GetPos())
						util.Effect("bloodspray", effectdata,true,true)
						for i=1,math.random(3,4) do
							local effectdata = EffectData()
							effectdata:SetOrigin(ragdoll:GetPos() + Vector(0,0,60))
							util.Effect("headshot_gib", effectdata,true,true)
						end
					end
				else
					//v.Player:AddPoints(Points[self.Name])
					self:SpawnPlutonium(Points[self.Name],v.Player)
				end
				v.Player:AddAwardPoints(AWARD_GOOD_KILLS,1)
				v.Player:AddAwardPoints(AWARD_BAD_KILLS,1)
			end
		end
	end
end

function ENT:SpawnPlutonium(amt,ply)
	for i=1,amt do
		local plut = ents.Create("sent_plutonium")
		plut:SetPos(self:GetPos() + Vector(0,0,math.random(30,60)))
		plut:SetAngles(Angle(math.random(-100,100),math.random(-100,100),math.random(-100,100)))
		plut:Spawn()
		if not ply:Alive() or ply:Team() != TEAM_HUMANS then
			if team.NumPlayers(TEAM_HUMANS) > 1 then
				plut:SetTarget(ChooseRandom(team.GetPlayers(TEAM_HUMANS)))
			else
				plut:SetTarget(ply)
			end
		else
			plut:SetTarget(ply)
		end
	end
end

local function ScaleBone( ent, realboneid, Size )

	local matBone = ent:GetBoneMatrix( realboneid )
	//matBone:Scale( Vector( -10, -10, -10 ) )
	matBone:Scale( Vector( Size, Size, Size ) )

	ent:SetBoneMatrix( realboneid, matBone )

end

function ENT:DoDeath(dmginfo)

	if self.IsDead then self:Remove() return end
	self.IsDead = true

	local killer = dmginfo:GetAttacker()

	self:VoiceSound(ChooseRandom(self.Death),100)
			
	local ragdoll = ents.Create("prop_ragdoll")
	ragdoll:SetKeyValue("angles", self:GetAngles().p + math.random(-3,3).." "..self:GetAngles().y +  math.random(-3,3).." "..self:GetAngles().r + math.random(-3,3))
	ragdoll:SetPos(self:GetPos())
	ragdoll:SetModel(self:GetModel())
	ragdoll:Spawn()
	ragdoll:Activate()
	ragdoll:Fire("kill",1,math.random(10,15))
	ragdoll:SetCollisionGroup( COLLISION_GROUP_DEBRIS )
	
	for i=1,20 do
		local bone = ragdoll:GetPhysicsObjectNum( i )
		if ValidEntity( bone ) then
			local bonepos, boneang = self:GetBonePosition( self:TranslatePhysBoneToBone( i ) )
			bone:SetPos( bonepos )
			bone:SetAngle( boneang )
		end
	end
	
	self:CalculateKiller(dmginfo,ragdoll)
	
	if self.FireAttacker then
		self.FireAttacker:AddAwardPoints(AWARD_GOOD_PYRO,1)
	end
			
	if math.random(1,100) <= GameInfo.ZombieItemChance then
		local hpos = ragdoll:LookupAttachment(self.Hand)
		local att = ragdoll:GetAttachment(hpos)
		if att == nil then
			att = {}
			att.Ang = Angle(0,0,0)
			att.Pos = ragdoll:GetPos() + Vector(0,0,5)
		end
		local item = ents.Create("prop_physics_respawnable")
		item:SetPos(att.Pos)
		item:SetAngles(att.Ang)
		item:SetModel(ChooseRandom(Inventory.ZombieItems))
		item:Spawn()
		item:Activate()
		item:SetHealth(150)
	end
			
	if math.random(1,4) == 1 then
		killer:VoiceSound(ChooseRandom(killer.Taunt),100,.3)
	end
			
	local phys = ragdoll:GetPhysicsObject()
	if phys:IsValid() then
		phys:SetMass(50)
		local fnum = 900
		if dmginfo:IsExplosionDamage() then 
			fnum = phys:GetMass()^3.2
		elseif GameInfo.ExaggeratedDeath then
			fnum = phys:GetMass()^2.2
		else
			fnum = phys:GetMass()^2.1
		end
		phys:AddAngleVelocity(Angle(math.Rand(-1*(fnum/5),fnum/5),math.Rand(-1*(fnum/3),fnum/3),math.Rand(-1*(fnum/5),fnum/5)))
		if self.OnFire then
			phys:ApplyForceCenter((ragdoll:GetPos()-killer:GetPos())*math.random(10,20))
			local effectdata = EffectData()
			effectdata:SetEntity(ragdoll)
			util.Effect("corpse_fire", effectdata)
		else
			phys:ApplyForceCenter((ragdoll:GetPos()-killer:GetPos())*math.random(50,200))
		end
	end
			
	self:SetSchedule( SCHED_FALL_TO_GROUND )
	self:Remove()
end

function ENT:AddAttackerDamage(attacker,dmg,headshot)
	if not attacker:IsPlayer() then return end
	local cancalc = true
	local wep = attacker:GetActiveWeapon()
	if wep and wep != NULL then
		if wep:GetWorldModel() == "models/weapons/w_annabelle.mdl" or wep:GetWorldModel() == "models/weapons/w_shotgun.mdl" then
			cancalc = false
		end
	end
	if headshot and cancalc then
		if attacker.HSCombo then
			attacker.HSCombo = attacker.HSCombo + 1
			if attacker.HSCombo == 5 then
				Inventory.Notice(attacker, "5 headshot combo", Inventory.Yellow)
				//attacker:AddPoints(1)
				self:SpawnPlutonium(1,attacker)
			elseif attacker.HSCombo == 10 then
				Inventory.Notice(attacker, "10 headshot combo", Inventory.Yellow)
				//attacker:AddPoints(3)
				self:SpawnPlutonium(3,attacker)
			elseif attacker.HSCombo == 15 then
				Inventory.Notice(attacker, "15 headshot combo", Inventory.Yellow)
				//attacker:AddPoints(6)
				self:SpawnPlutonium(6,attacker)
			elseif attacker.HSCombo == 20 then
				Inventory.Notice(attacker, "20 headshot combo", Inventory.Yellow)
				//attacker:AddPoints(10)
				self:SpawnPlutonium(10,attacker)
				 attacker.HSCombo = 1
			end
		else
			attacker.HSCombo = 1
		end
	else
		attacker.HSCombo = 0
	end
	if not self.Attackers then
		self.Attackers = {}
		self.Attackers[1] = {Player=attacker, Damage=math.Round(dmg), Headshot=headshot}
	else
		if not self:FindAttackerDamage(attacker) then
			self.Attackers[table.Count(self.Attackers)+1] = {Player=attacker, Damage=math.Round(dmg), Headshot=headshot}
		else
			for k,v in pairs(self.Attackers) do
				if v.Player == attacker then
					v.Damage = math.Round(v.Damage or 0)
					v.Damage = math.Round(v.Damage + dmg)
					v.Headshot = headshot
				end
			end
		end
	end
end

function ENT:GetAttackerTable()
return self.Attackers or {} end

function ENT:FindAttackerDamage(attacker)
	if not self.Attackers then return nil end
	for k,v in pairs(self.Attackers) do
		if v.Player == attacker then
			return v.Damage, v.Headshot
		end
	end
	return nil
end

function ENT:OnTakeDamage(dmg)
	if not dmg:GetAttacker():IsPlayer() then
		dmg:SetDamage(0)
	end
	self:SetHealth(math.Clamp(self:Health() - dmg:GetDamage(),0,100))
	if self:Health() <= 0 then
		if self:IsValid() then
			self:DoDeath(dmg)
		end
	else
		local has = false
		local tbl = self:GetAttackerTable()
		for k,v in pairs(tbl) do
			if v.Player == dmg:GetAttacker() then
				has = true
			end
		end
		if has == false then
			self:AddAttackerDamage(dmg:GetAttacker(),dmg:GetDamage(),false)
		end
		if math.random(1,3) == 1 then
			self:VoiceSound(ChooseRandom(self.Pain),100)
		end
	end
end 

function ENT:Think()
	if not self.IdleTalk then
		self.IdleTalk = CurTime() + math.random(15,25)
		self.OldCheckPos = self:GetPos()
	elseif self.IdleTalk < CurTime() then
		self:VoiceSound(ChooseRandom(self.Taunt),100)
		self.IdleTalk = nil
		if self.OldCheckPos:Distance(self:GetPos()) < 5 then
			GAMEMODE:SpawnUndead(self:GetClass())
			self:Remove()
		end
	end
	self:DoOnFire()
	if not self.Locator then
		self.Locator = CurTime() + 2
	elseif self.Locator < CurTime() then
		self.Locator = nil
		if self.CurEnemy and self.CurEnemy != NULL then
			if self.CurEnemy:Team() == TEAM_HUMANS and self.CurEnemy:Alive() then
				self:SetEnemy( self.CurEnemy, true ) 
				self:UpdateEnemyMemory( self.CurEnemy, self.CurEnemy:GetPos() ) 
			else
				if team.NumPlayers(TEAM_HUMANS) < 1 then
					self:SetEnemy( NULL )
					self.CurEnemy = nil
				else
					local enemy = ChooseRandom(team.GetPlayers(TEAM_HUMANS))
					self:SetEnemy( enemy, true ) 
					self:UpdateEnemyMemory( enemy, enemy:GetPos() ) 
					self.CurEnemy = enemy
				end
			end
		end
	end
end

// Player should be on fire throughout x seconds
function ENT:SetFireTime( add )
	self.FireTimer = CurTime() + add 
end

// Returns the amount of seconds left being on fire
function ENT:GetFireTime( add )
	return (self.FireTimer or 0) - CurTime()
end

function ENT:DoExtinguish()
	self:SetFireTime( -1 )
	self.FireAttacker = nil
	self:SetNetworkedBool("onfire",false)
	self.BurnSound:Stop()
end

function ENT:NpcIgnite( attacker )
	if not self.FireAttacker then
		self:VoiceSound(Sound("ambient/fire/mtov_flame2.wav"),100)
		local burn = Sound("General.BurningFlesh")
		self.BurnSound = CreateSound(self, burn)
		self.BurnSound:Play()
	end
	self:SetFireTime( math.random(15,20) )
	if attacker == self or attacker == nil then
		self.FireAttacker = nil
		return
	end
	self.FireAttacker = attacker
end

function ENT:DoOnFire()
	local OnFire = self.OnFire

	if self:GetFireTime() <= 0 then
		if OnFire then
			self.OnFire = false
			self:SetNetworkedBool("onfire",false)
			self.BurnSound:Stop()
		end
	return end
	
	if not OnFire then
		self.OnFire = true
		self:SetNetworkedBool("onfire",true)
		
		local effectdata = EffectData()
		effectdata:SetEntity(self)
		effectdata:SetOrigin(self:GetPos())
		util.Effect("player_immolate", effectdata)
	end

	if (self.LastFire or 0) < CurTime() and self.OnFire then
	
		self.LastFire = CurTime() + 0.1 
		self.Interval = self.Interval or 1
		local rand = math.random(1,20)
		if not self.bInterval then
			self.bInterval = CurTime() + math.Rand(.5,1)
			self:VoiceSound(ChooseRandom(self.Pain),100)
		elseif self.bInterval < CurTime() then
			self.bInterval = nil
		end
		if self.FireAttacker then
			self:TakeDamage( 1, self.FireAttacker, self.FireAttacker )
			self:AddAttackerDamage(self.FireAttacker,1,false)
		else
			self:SetHealth(self:Health() - 1)
		end
	end
end

ENT.OldPos = Vector(0,0,0)
function ENT:TaskStart_FindEnemy( data ) 
	if self.OldPos:Distance(self:GetPos()) < 5 then
		if team.NumPlayers(TEAM_HUMANS) >= 1 then
			self.CurEnemy = ChooseRandom(team.GetPlayers(TEAM_HUMANS))
		else
			self.CurEnemy = NULL
			self:SetEnemy( self.CurEnemy, true ) 
			self:TaskComplete() 
			return
		end
		self.OldPos = self:GetPos()
		self:SetEnemy( self.CurEnemy, true ) 
		self:UpdateEnemyMemory( self.CurEnemy, self.CurEnemy:GetPos() ) 
		self:TaskComplete() 
		return
	end	
 	local enemies = team.GetPlayers(TEAM_HUMANS)
	if table.Count(enemies) >= 1 then
		if self.CurEnemy then
			if self.CurEnemy:Team() == TEAM_HUMANS and self.CurEnemy:Alive() then
				local dist = self.CurEnemy:GetPos():Distance(self:GetPos())
				local enemy = self.CurEnemy
				for k,v in pairs(enemies) do
					if v:GetPos():Distance(self:GetPos()) < dist then
						dist = v:GetPos():Distance(self:GetPos())
						enemy = v
					end
				end
				self.OldPos = self:GetPos()
				self.CurEnemy = enemy
				self:SetEnemy( enemy, true ) 
				self:UpdateEnemyMemory( enemy, enemy:GetPos() ) 
				self:TaskComplete() 
			else
				local enemy = ChooseRandom(enemies)
				self:SetEnemy( enemy, true ) 
				self:UpdateEnemyMemory( enemy, enemy:GetPos() ) 
				self.CurEnemy = enemy
				self:TaskComplete() 
			end
		else
			local enemy = NULL
			local dist = 999999
			for k,v in pairs(enemies) do
				if v:GetPos():Distance(self:GetPos()) < dist then
					dist = v:GetPos():Distance(self:GetPos())
					enemy = v
				end
			end
			self.OldPos = self:GetPos()
			self.CurEnemy = enemy
			self:SetEnemy( enemy, true ) 
			self:UpdateEnemyMemory( enemy, enemy:GetPos() ) 
			self:TaskComplete() 
		end
	else
		self:SetEnemy( NULL )	 
	end
end 

function ENT:TaskStart_PlaySequence( data )

	local SequenceID = data.ID

	if ( data.Name ) then SequenceID = self:LookupSequence( data.Name )	end

	self:ResetSequence( SequenceID )
	self:SetNPCState( NPC_STATE_SCRIPT )

	local Duration = self:SequenceDuration()

	if ( data.Speed && data.Speed > 0 ) then 

		SequenceID = self:SetPlaybackRate( data.Speed )
		Duration = Duration / data.Speed

	end

	self.TaskSequenceEnd = CurTime() + Duration
	self.HitTime = CurTime() + (Duration / self.AnimScale)
	self:EmitSound(ChooseRandom(self.Attack),100,math.random(85,100))

end

function ENT:Task_PlaySequence( data )
	
	if ( CurTime() > self.HitTime ) and not self.DidHit then
		self.DidHit = true
		
		local boards = ents.FindByModel("models/props_debris/wood_chunk03a.mdl")
		table.Add(boards,ents.FindByModel("models/props_debris/wood_chunk03b.mdl"))
		table.Add(boards,ents.FindByClass("func_breakable"))
		for k,v in pairs(boards) do
			if v:GetPos():Distance(self:GetPos()) < 150 then
				self:EmitSound(ChooseRandom(GameSounds.Claw),100,math.random(90,110))
				v:TakeDamage(self.Damage,self)
				v:EmitSound(ChooseRandom(GameSounds.WoodHit),100,math.random(90,110))
				if math.random(1,2) == 1 then
					self:VoiceSound(ChooseRandom(self.Taunt),100)
				end
			end
		end
		
		for k,v in pairs(team.GetPlayers(TEAM_HUMANS)) do
			if v:GetPos():Distance(self:GetPos()) < 50 then
				if v:KeyDown(IN_DUCK) then
					v:TakeDamage(self.Damage,self)
					v.DeathType = "normal"
					self:EmitSound(ChooseRandom(GameSounds.Claw),100,math.random(90,110))
				end
			end
		end
		
		for k,v in pairs(ents.FindByClass("prop_door_rotating")) do
			if v:GetPos():Distance(self:GetPos()) < 150 then
				self:EmitSound(ChooseRandom(GameSounds.Claw),100,math.random(90,110))
				if v.HitHealth then
					v.HitHealth = v.HitHealth - self.DoorDamage
					v:EmitSound(ChooseRandom(GameSounds.WoodHit),100,math.random(90,110))
					if v.HitHealth <= 0 then
						v:EmitSound(ChooseRandom(GameSounds.WoodBreak),100,math.random(90,110))
						v:Fire("unlock",1,.1)
						v:Fire("open",1,.1)
						Inventory.RemoveDoor(v)
					end
				else
					v.HitHealth = 20
				end
				if math.random(1,2) == 1 then
					self:VoiceSound(ChooseRandom(self.Taunt),100)
				end
			end
		end
		
		local trace = {}
		trace.start = (self:GetPos() + Vector(0,0,60)) + (self:GetAngles():Forward() * -10)
		trace.endpos = trace.start + self:GetAngles():Forward() * 110
		trace.filter = self
		local tr = util.TraceLine(trace) 
		if tr.HitWorld or not tr.Entity:IsValid() then 
			return
		end
		if tr.Entity:IsPlayer() then
			if tr.Entity:Team() == TEAM_HUMANS then
				self:EmitSound(ChooseRandom(GameSounds.Claw),100,math.random(90,110))
				tr.Entity:TakeDamage(math.Round(self.Damage*GameInfo.ZombieDamageScale),self,self)
				tr.Entity.DeathType = "normal"
				if math.random(1,3) == 1 then
					self:VoiceSound(ChooseRandom(self.Taunt),100)
				end
				if math.random(1,2) == 1 and self.Bloody then
					tr.Entity:DoBleed()
				elseif math.random(1,3) != 1 and self.Infect then
					tr.Entity:DoInfection()
					tr.Entity:AddAwardPoints(AWARD_BAD_INFECT,1)
				elseif self.Poison then
					tr.Entity:DoPoison()
					tr.Entity:AddAwardPoints(AWARD_BAD_INFECT,1)
				end
			else
				self:EmitSound(ChooseRandom(GameSounds.Claw),100,math.random(90,110))
			end
		elseif string.find(tr.Entity:GetClass(),"prop_phys") then
			self:EmitSound(ChooseRandom(GameSounds.Claw),100,math.random(90,110))
			tr.Entity:TakeDamage(math.Round(self.Damage*GameInfo.ZombieDamageScale),self,self)
			local phys = tr.Entity:GetPhysicsObject()
			if phys:IsValid() then
				phys:ApplyForceCenter(self:GetForward() * 2000)
			end
		else
			self:EmitSound(ChooseRandom(GameSounds.Throw),100,math.random(90,110))
		end
	end
	
	if CurTime() < self.TaskSequenceEnd then return end
	
	self:TaskComplete()
	self.DidHit = false
	self:SetNPCState( NPC_STATE_NONE )

	// Clean up
	self.TaskSequenceEnd = nil

end
